//
//  MyViewController.m
//  HQJFacilitator
//
//  Created by mymac on 16/9/20.
//  Copyright © 2016年 Fujian first time iot technology investment co., LTD. All rights reserved.
//

#import "MyViewController.h"
#import "DetailViewController.h"
#import "VipViewController.h"
#import "TabebalViewHeader.h"
#import "MyModel.h"
#import "MyTableViewCell.h"
#import "MyViewModel.h"
#import "LoginViewController.h"
#import "MyModel.h"
#import "SDCycleScrollView.h"
#import "TabBarBannerViewModel.h"
#import "TabBarBannerModel.h"
#define TABBLECELL  @"TableViewCell"
//#import <UIKit/B>
@interface MyViewController ()<UITableViewDelegate,UITableViewDataSource,UIScrollViewDelegate,SDCycleScrollViewDelegate>
@property (nonatomic,strong)UITableView *MyTabbaleView;
@property (nonatomic,strong) MyViewModel *viewModel;
@property (nonatomic,strong)TabebalViewHeader *BjHeaderView;
@property (nonatomic,strong) MyModel * model;



/*********** banner 图片地址  ************/
@property (nonatomic,strong) NSMutableArray *images;
@end

@implementation MyViewController

-(UITableView *)MyTabbaleView {
    if (!_MyTabbaleView) {
        _MyTabbaleView = [[UITableView alloc]init];
        _MyTabbaleView.frame = CGRectMake(0, 0, WIDTH, HEIGHT);
        _MyTabbaleView.backgroundColor = [UIColor groupTableViewBackgroundColor];
        _MyTabbaleView.delegate = self;
        _MyTabbaleView.dataSource = self;
        [_MyTabbaleView registerClass:[UITableViewCell class] forCellReuseIdentifier:TABBLECELL];
        [_MyTabbaleView registerClass:[MyTableViewCell class] forCellReuseIdentifier:NSStringFromClass([MyTableViewCell class])];
        _MyTabbaleView.tableFooterView = [UIView new];

    }
    
    return _MyTabbaleView;
}


-(void)headerView {
    
   
    
}


-(void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
   

    
}
-(void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
//    [[NSNotificationCenter defaultCenter]removeObserver:self];
 
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.images = [NSMutableArray array];
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeBonus) name:@"changeBonus" object:nil];
//    self.fd_prefersNavigationBarHidden = YES;
    self.view.backgroundColor = [UIColor redColor];
    
    
    [self.view addSubview:self.MyTabbaleView];
    
  

        [self outsideRequst];
   
    
}
-(void)changeBonus {
    [self outsideRequst];

    
}
-(void)dealloc {
   
    [[NSNotificationCenter defaultCenter]removeObserver:self];
    
    [_model removeObserver:self forKeyPath:@"imageArray"];
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return 10;
    
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *headerView = [[UIView alloc]init];
    headerView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    return headerView;
    
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
        return 3;
  
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if (section != 2) {
        return 2;
    } else {
        
        return 1;

    }
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
   
    

        MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([MyTableViewCell class]) forIndexPath:indexPath];
        
        /**从模型中获取内容**/
        NSString *imageStr = [MyModel imageViewArraySeciton:indexPath.section][indexPath.row];
        NSString *titleStr = [MyModel titleArraySection:indexPath.section][indexPath.row];
        
        
        cell.titleImageView.image =[UIImage imageNamed:imageStr];
        cell.titleLabel.text = titleStr;
        
        /***右侧小箭头***/
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
     
        return cell;

  
    
    

}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    if (section == 2) {
        return 1;
    }
    
    return 0;
}


/**
 *  表头视图
 *
 */
-(UIView *)tabbleViewHeaderView {

    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT*180/568 + 44)];
    // 网络加载 --- 创建带标题的图片轮播
    SDCycleScrollView *cycleScrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT*180/568) delegate:self placeholderImage:[UIImage imageNamed:@"default_images"]];
    

 
    
    cycleScrollView.imageURLStringsGroup = self.images;
    
    cycleScrollView.pageControlAliment = SDCycleScrollViewPageContolAlimentRight;
    
    // 自定义分页控件小圆标颜色
    cycleScrollView.currentPageDotColor = [UIColor whiteColor];
    
    cycleScrollView.pageControlAliment = SDCycleScrollViewPageContolAlimentCenter;
//    }
//    HQJLog(@"数组：\n%@",_images);

    [view addSubview:cycleScrollView];

    
    

    
    // 轮播图下面的当日互动背景图
    
    UIView *cellOneBackgroundView =  [[UIView alloc]init];
    view.backgroundColor = [UIColor whiteColor];

    [view addSubview:cellOneBackgroundView];
    
    cellOneBackgroundView.sd_layout.leftSpaceToView(view,0).topSpaceToView(view,HEIGHT*180/568).heightIs(44).widthIs(WIDTH);
    
    
    NSString *bonusStr = [FileEngine filePathNameReadName:@"bonus" andstye:fileHomeDataStyle];

    NSString *string = [NSString stringWithFormat:@"当日互动：%@ 积分",bonusStr];
   
        
        
        //当日互动
     
            
        UILabel *cellOneLabel = [[UILabel alloc]init];
       
        cellOneLabel.text = string;
    
    
    
        [cellOneBackgroundView addSubview:cellOneLabel];

        CGFloat cellOneWidth = [ManagerEngine setTextWidthStr:string andFont:[UIFont systemFontOfSize:17.0]];

        cellOneLabel.sd_layout.leftSpaceToView(cellOneBackgroundView,15).topSpaceToView(cellOneBackgroundView,(44 - 17)/2).heightIs(17).widthIs(cellOneWidth);
        
    
            NSString *stringForColor = [NSString stringWithFormat:@"%@",bonusStr];
   
            [ManagerEngine setTextColor:cellOneLabel FontNumber:[UIFont systemFontOfSize:17.0] AndRange:stringForColor AndColor:[ManagerEngine getColor:@"00ccb8"]];

    
    NSString *mobileStr = [ [FileEngine filePathNameReadName:@"mobile" andstye:fileDefaultStyle] stringByReplacingCharactersInRange:NSMakeRange(3, 4) withString:@"****"];
    
    if ([FileEngine filePathNameReadName:@"realname" andstye:fileDefaultStyle] && mobileStr) {
        self.navigationItem.title  = [NSString stringWithFormat:@"%@(%@)",[FileEngine filePathNameReadName:@"realname" andstye:fileDefaultStyle],mobileStr];

    }


    return view;
}

-(void)outsideRequst {
    _viewModel = [[MyViewModel alloc]init];
    [_viewModel myRequst:^{
        self.MyTabbaleView.tableHeaderView = [self tabbleViewHeaderView];

//        [self.MyTabbaleView reloadData];
    }];
    [self BannerRequst];
    
}

-(void)BannerRequst {
    
    [TabBarBannerViewModel pictureRequst:^(id sender) {
        
        if (self.images.count == 0) {
            for (TabBarBannerModel *pictureModels in sender ) {
                
                [self.images addObject:[NSString stringWithFormat:@"%@%@",IMAGEURL,pictureModels.src]];
               
            }
        }
        
        
    }];
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
   NSString *titStr  =[MyModel titleArraySection:indexPath.section][indexPath.row];
    [_viewModel ClickCellIndexPath:indexPath andViewController:self andTit:titStr];
    
    
}




@end
